package com.sskj.mine.ui.activity;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.CodeCheckDialogActivityPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.MINE_CODE_CHECK)//验证码验证下部弹窗
public class CodeCheckDialogActivity extends BaseActivity<CodeCheckDialogActivityPresenter> {
    @BindView(R2.id.tvCancel)
    TextView tvCancel;
    @BindView(R2.id.tvAccount)
    TextView tvAccount;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R2.id.btSure)
    TextView btSure;
    @Autowired
    String phone;
    DisposableSubscriber disposableSubscriber;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_code_check_dialog;
    }

    @Override
    public boolean getOrientation() {
        return false;
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    public CodeCheckDialogActivityPresenter getPresenter() {
        return new CodeCheckDialogActivityPresenter();
    }

    private void setWindow() {
        //窗口对齐屏幕宽度
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;//设置对话框置顶显示
        win.setAttributes(lp);
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setWindow();
        if (CheckUtil.isMobile(phone)) {
            String start = phone.substring(0, 3);
            String end = phone.substring(7, 11);
            String phone = start + "****" + end;
            tvAccount.setText(phone);
        } else {
            etCode.setHint(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity1));
            tvAccount.setText(phone);
        }
        startCount();

        ClickUtil.click(btSure, () -> {
            if (!TextUtils.isEmpty(etCode.getText())) {
                setPwd(phone);
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity2));
            }
        });
        ClickUtil.click(tvGetCode, () -> {
            if (CheckUtil.isMobile(phone)) {
                mPresenter.sendCode(phone);
            } else {
                mPresenter.sendEmailCode(phone);
            }
        });
        ClickUtil.click(tvCancel, () -> finish());
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    btSure.setEnabled(true);
                } else {
                    btSure.setEnabled(false);
                }
            }
        });
    }

    public void startCount() {
        tvGetCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvGetCode.setText(time + App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity3));
                tvGetCode.setTextColor(getResources().getColor(R.color.mine_adc));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvGetCode.setText(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity4));
                tvGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposableSubscriber != null && !disposableSubscriber.isDisposed()) {
            disposableSubscriber.dispose();
            disposableSubscriber = null;
        }
    }

    public void setPwd(String account) {
        ARouter.getInstance().build(RConfig.MINE_PWD_RESET)
                .withString(Constans.PHONE, account)
                .withString(Constans.CODE, etCode.getText().toString())
                .navigation();
    }

}
